package com.cardone.platform.common.service;

import com.cardone.common.Characters;
import com.cardone.common.cache.util.CacheUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.common.dao.EntityLogDao;
import com.cardone.platform.common.dto.EntityLogDto;
import com.cardone.platform.configuration.util.DictionaryUtils;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 实体日志服务
 *
 * @author yaohaitao
 */
@Getter
@Setter
@Transactional(readOnly = true)
public class EntityLogDefaultService extends com.cardone.common.service.SimpleDefaultService<com.cardone.platform.common.dto.EntityLogDto> implements EntityLogService {
    @Override
    public EntityLogDao getDao() {
        return ContextHolder.getBean(EntityLogDao.class);
    }

    @Override
    @Transactional
    public int insertByCode(final com.cardone.platform.common.dto.EntityLogDto insertEntityLog) {
        if (StringUtils.isBlank(insertEntityLog.getTypeId())) {
            String typeId = ContextHolder.getBean(com.cardone.platform.configuration.service.DictionaryService.class).readIdByTypeCodeAndCode(insertEntityLog.getTypeTypeCode(), insertEntityLog.getTypeCode());

            if (StringUtils.isBlank(typeId)) {
                typeId = StringUtils.join(insertEntityLog.getTypeTypeCode(), Characters.comma.stringValue(), insertEntityLog.getTypeCode());
            }

            insertEntityLog.setTypeId(typeId);
        }

        return ContextHolder.getBean(EntityLogDao.class).insertByCode(insertEntityLog);
    }

    @Override
    public List<Map<String, Object>> findListByEntityIdAndTopN(String entityId, Integer topN) {
        return this.getDao().findListByEntityIdAndTopN(entityId, topN);
    }

    @Override
    @Transactional
    public <P> P saveByIdOrCode(Class<P> mappedClass, EntityLogDto save, String... selectNames) {
        if (StringUtils.isBlank(save.getTypeId())) {
            String typeId = ContextHolder.getBean(com.cardone.platform.configuration.service.DictionaryService.class).readIdByTypeCodeAndCode(save.getTypeTypeCode(), save.getTypeCode());

            if (StringUtils.isBlank(typeId)) {
                typeId = StringUtils.join(save.getTypeTypeCode(), Characters.comma.stringValue(), save.getTypeCode());
            }

            save.setTypeId(typeId);
        }

        return super.saveByIdOrCode(mappedClass, save, selectNames);
    }

    @Override
    @Transactional(readOnly = true)
    public String readEntityIdByIdForValid(final String id) {
        return CacheUtils.get(EntityLogService.class.getName(), "readEntityIdByIdForValid:" + id, () -> com.cardone.context.ContextHolder.getBean(com.cardone.platform.common.dao.EntityLogDao.class).readEntityIdByIdForValid(id));
    }

    @Override
    @Transactional
    public int updateByClient(String clientId, String id, String dataStateCode) {
        String dataStateId = DictionaryUtils.readIdByTypeCodeAndCode("DATA_STATE_TYPE", dataStateCode);

        return this.getDao().updateByClient(clientId, id, dataStateId);
    }
}
